package org.dromara.datacheck.controller.jst;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.datacheck.domain.bo.JstAndJDTableDataEntryBo;
import org.dromara.datacheck.domain.bo.jst.JstPurchaseEnterWarehouseOrderBo;
import org.dromara.datacheck.domain.jst.JstPurchaseEnterWarehouseOrder;
import org.dromara.datacheck.domain.vo.jst.JstPurchaseEnterWarehouseOrderImportVo;
import org.dromara.datacheck.domain.vo.jst.JstPurchaseEnterWarehouseOrderVo;
import org.dromara.datacheck.listener.jst.JstPurchaseEnterWarehouseOrderImportListener;
import org.dromara.datacheck.service.jst.IJstPurchaseEnterWarehouseOrderService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * 聚水潭-采购入库单
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataCheck/jst/purchaseEnterWarehouseOrder")
public class JstPurchaseEnterWarehouseOrderController extends BaseController {

    private final IJstPurchaseEnterWarehouseOrderService jstPurchaseEnterWarehouseOrderService;

    /**
     * 查询聚水潭-采购入库单列表
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:list")
    @GetMapping("/list")
    public TableDataInfo<JstPurchaseEnterWarehouseOrderVo> list(JstPurchaseEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        return jstPurchaseEnterWarehouseOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聚水潭-采购入库单列表
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:export")
    @Log(title = "聚水潭-采购入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JstPurchaseEnterWarehouseOrderBo bo, HttpServletResponse response) {
        List<JstPurchaseEnterWarehouseOrderVo> list = jstPurchaseEnterWarehouseOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "聚水潭-采购入库单", JstPurchaseEnterWarehouseOrderVo.class, response);
    }

    /**
     * 获取聚水潭-采购入库单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:query")
    @GetMapping("/{id}")
    public R<JstPurchaseEnterWarehouseOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jstPurchaseEnterWarehouseOrderService.queryById(id));
    }

    /**
     * 新增聚水潭-采购入库单
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarhouseOrder:add")
    @Log(title = "聚水潭-采购入库单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JstPurchaseEnterWarehouseOrderBo bo) {
        return toAjax(jstPurchaseEnterWarehouseOrderService.insertByBo(bo));
    }

    /**
     * 修改聚水潭-采购入库单
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:edit")
    @Log(title = "聚水潭-采购入库单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JstPurchaseEnterWarehouseOrderBo bo) {
        return toAjax(jstPurchaseEnterWarehouseOrderService.updateByBo(bo));
    }

    /**
     * 删除聚水潭-采购入库单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:remove")
    @Log(title = "聚水潭-采购入库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jstPurchaseEnterWarehouseOrderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "聚水潭-采购入库单导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        // 每次导入之前清空表里的数据
        List<JstPurchaseEnterWarehouseOrder> jstPurchaseEnterWarhouseOrderList = jstPurchaseEnterWarehouseOrderService.queryAll();
        if(CollUtil.isNotEmpty(jstPurchaseEnterWarhouseOrderList)){
            jstPurchaseEnterWarehouseOrderService.deleteAll();
        }
        // 解析Excel文件并处理导入逻辑
        ExcelResult<JstPurchaseEnterWarehouseOrderImportVo> result = ExcelUtil.importExcel(file.getInputStream(), JstPurchaseEnterWarehouseOrderImportVo.class, new JstPurchaseEnterWarehouseOrderImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 查询聚水潭-采购入库单列表
     * 业务查询条件：根据operator_warehouse_owner操作仓储方名称为国内工厂在途仓和深圳/东莞仓查询
     * 场景分类：1、采购流程（含委外）-东莞仓，国内在途仓
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:list")
    @GetMapping("/list-by-dongguan-and-guonei")
    public TableDataInfo<JstPurchaseEnterWarehouseOrderVo> listByDongGuanAndGuoNei(JstPurchaseEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        bo.setOperatorWarhouseOwnerList(Arrays.asList("深圳/东莞仓", "国内工厂在途仓"));
        return jstPurchaseEnterWarehouseOrderService.queryPageListByDongGuanAndGuoNei(bo, pageQuery);
    }

    /**
     * 查询聚水潭-采购入库单列表以及金蝶-采购入库单数据汇总（按日期汇总）
     * 场景分类：1、采购流程（含委外）-东莞仓，国内在途仓
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:list")
    @GetMapping("/jst-and-jd-sum-by-date")
    public TableDataInfo<JstAndJDTableDataEntryBo> jstAndJdSumByDate() throws ParseException {
        return jstPurchaseEnterWarehouseOrderService.jstAndJdSumByDate();
    }

    /**
     * 查询聚水潭-采购入库单列表（金蝶缺少的数据）
     * 业务查询条件：根据operator_warehouse_owner操作仓储方名称为国内工厂在途仓和深圳/东莞仓查询
     * 场景分类：1、采购流程（含委外）-东莞仓，国内在途仓
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:list")
    @GetMapping("/list-by-dongguan-and-guonei-by-jd-deficiency")
    public TableDataInfo<JstPurchaseEnterWarehouseOrderVo> listByDongGuanAndGuoNeiByJdDeficiency1(JstPurchaseEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        return jstPurchaseEnterWarehouseOrderService.listByDongGuanAndGuoNeiByJdDeficiency1(bo, pageQuery);
    }

    /**
     * 导出聚水潭-采购入库单列表（金蝶缺少的数据）
     */
    @SaCheckPermission("dataCheck:purchaseEnterWarehouseOrder:export")
    @Log(title = "聚水潭-采购入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export-by-dongguan-and-guonei-by-jd-deficiency")
    public void exportByDongguanAndGuoneiByJdDeficiency(JstPurchaseEnterWarehouseOrderBo bo, HttpServletResponse response) {
        List<JstPurchaseEnterWarehouseOrderVo> list = jstPurchaseEnterWarehouseOrderService.queryListByDongguanAndGuoneiByJdDeficiency(bo);
        ExcelUtil.exportExcel(list, "聚水潭-采购入库单", JstPurchaseEnterWarehouseOrderVo.class, response);
    }
}
